package com.qxzn.app.page.model;

import android.content.Context;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.qxzn.app.api.api.HttpRequest;
import com.qxzn.app.bean.BaseBean;
import com.qxzn.app.bean.FaceDetectionBean;
import com.qxzn.app.bean.KeyBean;
import com.qxzn.app.page.interfaces.MyInterface;
import com.qxzn.app.util.AppUtil;

import java.io.File;

import okhttp3.MultipartBody;
import okhttp3.RequestBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class LoginAndRegisterModel {

    private Context context;
    private MyInterface.LoginListener loginListener;
    private MyInterface.RegisterListener registerListener;
    private MyInterface.GetCodeListener getCodeListener;
    private MyInterface.VerifyCodeListener verifyCodeListener;
    private MyInterface.OnFaceDetectionListener onFaceDetectionListener;
    private MyInterface.OnCheckRegisterListener onCheckRegisterListener;
    private MyInterface.UpdatePwdListener updatePwdListener;

    public LoginAndRegisterModel(Context context) {
        this.context = context;
    }

    /**
     * 登录
     *
     * @param params
     */
    public void login(JSONObject params) {
        HttpRequest.AUTHORIZE_KEY = "";
        RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"), JSON.toJSONString(params));
        HttpRequest.getService().login(body).enqueue(new Callback<BaseBean<KeyBean.DataBean>>() {
            @Override
            public void onResponse(Call<BaseBean<KeyBean.DataBean>> call, Response<BaseBean<KeyBean.DataBean>> response) {
                BaseBean<KeyBean.DataBean> baseBean = response.body();
                if (loginListener != null) {
                    loginListener.onLoginSuccess(baseBean);
                }
                if (baseBean != null && baseBean.getData() != null) {
                    HttpRequest.AUTHORIZE_KEY = baseBean.getData().getAppkey() + "-" + baseBean.getData().getInfokey();
                    AppUtil.showLog("key：" + HttpRequest.AUTHORIZE_KEY);
                }
            }

            @Override
            public void onFailure(Call<BaseBean<KeyBean.DataBean>> call, Throwable t) {
                if (loginListener != null) {
                    loginListener.onLoginFailure();
                }
            }
        });
    }

    /**
     * 注册
     *
     * @param params
     */
    public void register(JSONObject params, File file) {
        RequestBody phone = HttpRequest.getPartBody(params.getString("phone"));
        RequestBody nickname = HttpRequest.getPartBody(params.getString("nickname"));
        RequestBody pwd = HttpRequest.getPartBody(params.getString("pwd"));
        RequestBody security_pwd = HttpRequest.getPartBody(params.getString("security_pwd"));
        RequestBody gender = HttpRequest.getPartBody(params.getString("sex"));
        RequestBody birthday = HttpRequest.getPartBody(params.getString("birth"));
        RequestBody stature = HttpRequest.getPartBody(params.getString("height"));
        RequestBody weight = HttpRequest.getPartBody(params.getString("weight"));
        MultipartBody.Part fileBody = HttpRequest.getPartFileBody(file);
        HttpRequest.getService().register(phone, nickname, pwd, security_pwd, gender, birthday, stature, weight, fileBody).enqueue(new Callback<BaseBean<KeyBean.DataBean>>() {
            @Override
            public void onResponse(Call<BaseBean<KeyBean.DataBean>> call, Response<BaseBean<KeyBean.DataBean>> response) {
                BaseBean<KeyBean.DataBean> baseBean = response.body();
                if (registerListener != null) {
                    registerListener.onRegisterSuccess(baseBean);
                }
            }

            @Override
            public void onFailure(Call<BaseBean<KeyBean.DataBean>> call, Throwable t) {
                if (registerListener != null) {
                    registerListener.onRegisterFailure();
                }
            }
        });
    }

    /**
     * 获取验证码
     * 参数type类型： 注册为1 忘记密码为2 登陆为3
     *
     * @param params
     */
    public void getCode(JSONObject params) {
        RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"), JSON.toJSONString(params));
        HttpRequest.getService().getCode(body).enqueue(new Callback<BaseBean>() {
            @Override
            public void onResponse(Call<BaseBean> call, Response<BaseBean> response) {
                BaseBean baseBean = response.body();
                if (getCodeListener != null) {
                    getCodeListener.onGetCodeSuccess(baseBean);
                }
            }

            @Override
            public void onFailure(Call<BaseBean> call, Throwable t) {
                if (getCodeListener != null) {
                    getCodeListener.onGetCodeFailure();
                }
            }
        });
    }

    /**
     * 验证短信验证码是否正确
     *
     * @param params
     */
    public void verifyCode(JSONObject params) {
        RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"), JSON.toJSONString(params));
        HttpRequest.getService().verifyCode(body).enqueue(new Callback<BaseBean>() {
            @Override
            public void onResponse(Call<BaseBean> call, Response<BaseBean> response) {
                BaseBean baseBean = response.body();
                if (verifyCodeListener != null) {
                    verifyCodeListener.onVerifyCodeSuccess(baseBean);
                }
            }

            @Override
            public void onFailure(Call<BaseBean> call, Throwable t) {
                if (verifyCodeListener != null) {
                    verifyCodeListener.onVerifyCodeFailure();
                }
            }
        });
    }

    /**
     * 人脸检测
     *
     * @param faceFile
     */
    public void faceDetection(File faceFile) {
        MultipartBody.Part fileBody = HttpRequest.getPartFileBody(faceFile);
//        RequestBody device_id = HttpRequest.getPartBody(SharePreferenceUtil.getDeviceId(context));
        HttpRequest.getService().faceDetection(/*device_id, */fileBody).enqueue(new Callback<BaseBean<FaceDetectionBean>>() {
            @Override
            public void onResponse(Call<BaseBean<FaceDetectionBean>> call, Response<BaseBean<FaceDetectionBean>> response) {
                if (onFaceDetectionListener != null) {
                    onFaceDetectionListener.onFaceDetectionSuccess(response.body());
                }
            }

            @Override
            public void onFailure(Call<BaseBean<FaceDetectionBean>> call, Throwable t) {
                if (onFaceDetectionListener != null) {
                    onFaceDetectionListener.onFaceDetectionFailure();
                }
            }
        });
    }

    /**
     * 验证手机号是否已经被注册
     *
     * @param params
     */
    public void checkPhoneRegister(JSONObject params) {
        RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"), JSON.toJSONString(params));
        HttpRequest.getService().checkPhoneRegister(body).enqueue(new Callback<BaseBean>() {
            @Override
            public void onResponse(Call<BaseBean> call, Response<BaseBean> response) {
                if (onCheckRegisterListener != null) {
                    onCheckRegisterListener.onCheckRegisterSuccess(response.body());
                }
            }

            @Override
            public void onFailure(Call<BaseBean> call, Throwable t) {
                if (onCheckRegisterListener != null) {
                    onCheckRegisterListener.onCheckRegisterFailure();
                }
            }
        });
    }

    /**
     * 修改或重置密码
     * type 修改密码为1 忘记（重置）密码为2
     *
     * @param params
     */
    public void updatePwd(JSONObject params) {
        RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"), JSON.toJSONString(params));
        HttpRequest.getService().updatePwd(body).enqueue(new Callback<BaseBean>() {
            @Override
            public void onResponse(Call<BaseBean> call, Response<BaseBean> response) {
                if (updatePwdListener != null) {
                    updatePwdListener.onUpdatePwdSuccess(response.body());
                }
            }

            @Override
            public void onFailure(Call<BaseBean> call, Throwable t) {
                if (updatePwdListener != null) {
                    updatePwdListener.onUpdatePwdFailure();
                }
            }
        });
    }

    /**
     * 在页面关闭时移除监听
     */
    public void removeListener() {
        this.loginListener = null;
        this.registerListener = null;
        this.getCodeListener = null;
        this.verifyCodeListener = null;
        this.onFaceDetectionListener = null;
        this.onCheckRegisterListener = null;
        this.updatePwdListener = null;
    }

    public void setLoginListener(MyInterface.LoginListener loginListener) {
        this.loginListener = loginListener;
    }

    public void setRegisterListener(MyInterface.RegisterListener registerListener) {
        this.registerListener = registerListener;
    }

    public void setGetCodeListener(MyInterface.GetCodeListener getCodeListener) {
        this.getCodeListener = getCodeListener;
    }

    public void setVerifyCodeListener(MyInterface.VerifyCodeListener verifyCodeListener) {
        this.verifyCodeListener = verifyCodeListener;
    }

    public void setOnFaceDetectionListener(MyInterface.OnFaceDetectionListener onFaceDetectionListener) {
        this.onFaceDetectionListener = onFaceDetectionListener;
    }

    public void setOnCheckRegisterListener(MyInterface.OnCheckRegisterListener onCheckRegisterListener) {
        this.onCheckRegisterListener = onCheckRegisterListener;
    }

    public void setUpdatePwdListener(MyInterface.UpdatePwdListener UpdatePwdListener) {
        this.updatePwdListener = UpdatePwdListener;
    }
}
